package net
{
	import Common.ActionManager;
	import Common.GameAction;
	import Common.GameDefine;
	import Common.GameNetEvent;
	import Common.GameNetManager;
	import Common.GameResManager;
	import Common.MapDirection;
	import Common.RoleConfig;
	
	import battle.BattleCreature;
	
	import config.EnemyConfig;
	import config.UIConfig;
	
	import flash.utils.ByteArray;
	
	import logic.GameCreatureData;
	
	import map.MapGrid;
	
	import ui.ChatUIHandler;

	public class MapNetHandler
	{
		public function MapNetHandler()
		{
		}
		
		public static function onRecvJoinTeam( data:ByteArray ):void
		{
			var id:int = data.readInt();
			var leader:GamePlayer = GameCreatureManager.instance.getPlayer( id );
			
			var player:GamePlayer = GameCreatureManager.instance.getPlayer( data.readInt() );
			
			player.data.HP = data.readShort();
			player.data.MAXHP = data.readShort();
			player.data.MP = data.readShort();
			player.data.MAXMP = data.readShort();
			
			if ( !leader.team )
			{
				leader.team = new GameTeam();
				leader.team.init( leader );
				leader.team.join( leader );
			}
			
			player.team = leader.team;
			player.team.join( player );
			
			ChatUIHandler.instance.addText( player.Name + UIConfig.instance.getString( "JoinTeam" ) );
		}
		
		public static function onRecvSynTeam( data:ByteArray ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			var count:int = data.readInt();
			
			for ( var i:int = 0 ; i < count ; i++ )
			{
				var id:int = data.readInt();
				var player:GamePlayer = GameCreatureManager.instance.getPlayer( id );
				
				player.data.HP = data.readShort();
				player.data.MAXHP = data.readShort();
				player.data.MP = data.readShort();
				player.data.MAXMP = data.readShort();
				
				if ( !player.team )
				{
					player.team = new GameTeam();
					player.team.init( player );
					player.team.join( player );
					
					ChatUIHandler.instance.addText( player.Name + UIConfig.instance.getString( "JoinTeam" ) );
				}
			}
			
			mainPlayer.team = player.team;
			mainPlayer.team.join( mainPlayer );
		}
		
		public static function onRecvLeaveTeam( data:ByteArray ):void
		{
			var id:int = data.readInt();
			var player:GamePlayer = GameCreatureManager.instance.getPlayer( id );
			
			var team:GameTeam = player.team;
			team.leave( player );
			player.team = null;
			
			if ( player == team.leader || team.members.length == 1 )
			{
				for ( var i:int = 0 ; i < team.members.length ; i++ )
				{
					team.members[i].team = null;
				}
				
				team.release();
				team = null;
			}
			
			ChatUIHandler.instance.addText( player.Name + UIConfig.instance.getString( "LeaveTeam" ) );
		}
		
		public static function onRecvSynTeamMap( data:ByteArray ):void
		{
			var count:int = data.readInt();
			
			for ( var i:int = 0 ; i < count ; i++ )
			{
				var num:int = data.readInt();
				var leader:int = data.readInt();
				
				var player:GamePlayer = GameCreatureManager.instance.getPlayer( leader );
				var team:GameTeam = new GameTeam();
				team.init( player );
				team.join( player );
				player.team = team;
				
				for ( var j:int = 1 ; j < num ; j++ )
				{
					var id:int = data.readInt();
					player = GameCreatureManager.instance.getPlayer( id );
					team.join( player );
					player.team = team;
				}
			}
		}
		
		public static function onRecvExitMap( data:ByteArray ):void
		{
			var id:int = data.readInt();
			
			var player:GamePlayer = GameCreatureManager.instance.getPlayer( id );
			
			if ( player )
			{
				if ( player.team && player.team.members.length )
				{
					for ( var i:int ; i < player.team.members.length ; i++ )
					{
						GameCreatureManager.instance.removePlayer( player.team.members[ i ].ID );
					}
				}
				else
				{
					GameCreatureManager.instance.removePlayer( id );
				}
			}
		}
		
		public static function onRecvMove( data:ByteArray ):void
		{
			var id:int = data.readInt();
			var mapID:int = data.readInt();
			var count:int = data.readInt();
			
			var player:GamePlayer = GameCreatureManager.instance.getPlayer( id );
			
			if ( !player ) 
			{
//				var startX1:int = data.readShort();
//				var startY1:int = data.readShort();
//				
//				for (var i1:int = 1; i1 < count; i1++) 
//				{
//					var x1:int = data.readShort();
//					var y1:int = data.readShort();
//				}
				
				return;
			}
			var startX:int = data.readShort();
			var startY:int = data.readShort();
			
			player.endMoving();
			player.setPos( startX , startY );
			
			for (var i:int = 1; i < count; i++) 
			{
				var x:int = data.readShort();
				var y:int = data.readShort();
				
				player.MovePath.push( new MapGrid( x , y ) );
			}
			
			player.startMoving();
		}
		
		
		
		public static function onRecvAction( data:ByteArray ):void
		{
			var guid:int = data.readInt();
			var actionID:int = data.readInt();
			
			var player:GamePlayer = GameCreatureManager.instance.getPlayer( guid );
			
			if ( player ) 
			{
				player.setAction( actionID );
			}
			
		}
		
		
		
		public static function onRecvDirection( data:ByteArray ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			var guid:int = data.readInt();
			var direction:int = data.readInt();
			
			var player:GamePlayer = GameCreatureManager.instance.getPlayer( guid );
			
			if ( player ) 
			{
				if ( player == mainPlayer ) 
				{
					return;
				}
				
				//if ( player.isMoving ) 
				//{
				//player.isDirectionBuffer = true;
				//player.directionBuffer = direction;
				//}
				//else
				//{
				//player.moveDirection = direction;
				//player.changeActionMC();
				//}
			}
			
		}
		
		
		public static function readPlayerInfo( player:GamePlayer , data:ByteArray ):void
		{
			player.init();
			
			player.data.ID = data.readInt();
			player.data.Name = data.readUTFBytes( 32 );
			player.data.Role = data.readShort();
			player.data.Color = data.readShort();
			player.data.Level = data.readByte();
			player.data.LevelPRO = data.readByte();
			data.readShort();
			
			player.data.Index = RoleConfig.instance.getIndex( player.data.Role , player.data.Color , GameDefine.INVALIDID );
			
			player.data.HP = data.readShort();
			player.data.MAXHP = data.readShort();
			player.data.MP = data.readShort();
			player.data.MAXMP = data.readShort();
			
			player.MapID = data.readInt();
			player.setPos( data.readShort() , data.readShort() );
			
			player.updateActionID();
			
			GameCreatureManager.instance.addPlayer( player );
		}
		
		public static function onRecvEnterMap( data:ByteArray ):void
		{
			var player:GamePlayer = new GamePlayer();
			readPlayerInfo( player , data );
			
			var count:int = data.readInt();
			
			if ( count )
			{
				var team:GameTeam = new GameTeam();
				team.init( player );
				team.join( player );
				player.team = team;
				
				for ( var i:int ; i < count ; i++ )
				{
					player = new GamePlayer();
					readPlayerInfo( player , data );
					
					team.join( player );
					player.team = team;
				}
			}
			
		}
		
		public static function onRecvMapInfo( data:ByteArray ):void
		{
			var count:int = data.readInt();
			
			for ( var i:int = 0; i < count; i++ ) 
			{
				var player:GamePlayer = new GamePlayer();
				readPlayerInfo( player , data );
			}
		}
		
		
		public static function onNpcEnterMap( data:ByteArray ):void
		{
			var count:int = data.readInt();
			
			for ( var i:int = 0 ; i < count ; i++ )
			{
				var npc:GameNPC = new GameNPC();
				npc.init();
				
				npc.textureID = data.readInt();
				npc.data.ID = data.readShort();
				ActionManager.instance.loadNPC( npc.textureID , data.readByte() , data.readByte() );

				npc.setPos( data.readShort() , data.readShort() );
				npc.data.Fall = data.readInt() == 1;
				npc.data.Name = data.readUTFBytes( GameDefine.MAX_NAME );
				
				GameCreatureManager.instance.addNpc( npc );
			}
		}
		
		
		public static function onEnterMap( data:ByteArray ):void
		{
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			mainPlayer.endMoving();
			mainPlayer.MapID = data.readInt();
			mainPlayer.setPos( data.readShort() , data.readShort() );
			
			MapManager.instance.Load( mainPlayer.MapID );
			GameScene.instance.move( mainPlayer.MMapGrid );
			
			GameCreatureManager.instance.clear();
			GameCreatureManager.instance.addPlayer( mainPlayer );
			
			
			// test ,,
//			for ( var j:int = 0 ; j < 30 ; j++ )
//			{
//				for ( var k:int = 0 ; k < 30 ; k++ )
//				{
//					var player:GamePlayer = new GamePlayer();
//					player.init();
//					
//					player.MapID = mainPlayer.MapID;
//					player.data.Index = 100001;
//					player.updateActionID();
//					player.setPos( mainPlayer.MMapGrid.x + j , mainPlayer.MMapGrid.y + k );
//					player.data.ID = j * 100 + k;
//					GameCreatureManager.instance.addPlayer( player );
//				}
//				
//			}
			
			if (  mainPlayer.team && mainPlayer.team.members.length )
			{
				for ( var i:int = 0 ; i < mainPlayer.team.members.length ; i++ )
				{
					if ( mainPlayer != mainPlayer.team.members[i] )
					{
						mainPlayer.team.members[i].MapID = mainPlayer.MapID;
						mainPlayer.team.members[i].setPos( mainPlayer.MMapGrid.x , mainPlayer.MMapGrid.y );
						
						GameCreatureManager.instance.addPlayer( mainPlayer.team.members[i] );
					}
				}
			}
			
		}
		
		public static function readCreature( data:ByteArray , bc:BattleCreature ):void
		{
			// 这里pvp不对，
			bc.data.HP = data.readShort();
			bc.data.MAXHP = data.readShort();
			
			bc.data.Level = data.readByte();
			bc.index = data.readByte();
			
			bc.data.Role = data.readByte();
			bc.data.Color = data.readByte();
			
			bc.data.Index = data.readInt();
			
			if ( bc.data.Index )
			{
				bc.data.Name = EnemyConfig.instance.getString( bc.data.Index ).@n;
			}
		}
		
		
		public static function onEnterBattle( data:ByteArray ):void
		{
			BattleSession.instance.clear();
			
			var mapID:int = data.readInt();
			var mapX:int = data.readShort();
			var mapY:int = data.readShort();
			var mainPlayer:GamePlayer = GameManager.instance.MainPlayer;
			
			if ( mainPlayer.team )
			{
				for( var i:int = 0 ; i < mainPlayer.team.members.length ; i++ )
				{
					mainPlayer.team.members[ i ].endMoving();
					mainPlayer.team.members[ i ].MapID = mapID;
					mainPlayer.team.members[ i ].setPos( mapX , mapY );
					
					GameCreatureManager.instance.updatePlayerScene( mainPlayer.team.members[ i ] );
				}
			}
			else
			{
				mainPlayer.endMoving();
				mainPlayer.MapID = mapID;
				mainPlayer.setPos( mapX , mapY );
				
				GameCreatureManager.instance.updatePlayerScene( mainPlayer );
			}
			
			GameScene.instance.move( mainPlayer.MMapGrid );
			
			var pvp:int = data.readByte();
			var direction:int = data.readByte();
			var numPet:int = data.readByte();
			var numEnemy:int = data.readByte();
			var numEnemyPet:int = data.readByte();
			var numPlayer:int = 1;
			
			var bc:BattleCreature = null;
			var mainPlayerIndex:int = 0;
			
			if ( mainPlayer.team )
			{
				numPlayer = mainPlayer.team.members.length;
				
				for ( i = 0 ; i < mainPlayer.team.members.length ; i++ )
				{
					bc = new BattleCreature();
					bc.init( mainPlayer.team.members[i].data );
					
					bc.setDirection( MapDirection.NORTH );
					bc.setActionID( mainPlayer.team.members[i].data.Gender );
					bc.setAction( GameAction.STAND , null );
					BattleSession.instance.addPlayer( bc );
					bc.index = i;
					
					if ( mainPlayer.team.members[i] == mainPlayer )
					{
						mainPlayerIndex = i;
					}
				}
			}
			else
			{
				bc = new BattleCreature();
				bc.init( mainPlayer.data );
				
				bc.setDirection( MapDirection.NORTH );
				bc.setActionID( mainPlayer.data.Index );
				bc.setAction( GameAction.STAND , null );
				
				BattleSession.instance.addPlayer( bc );
				bc.index = 0;
				mainPlayerIndex = 0;
				
//				if ( mainPlayer.attackPet )
//				{
//					bc = new BattleCreature();
//					bc.init( mainPlayer.attackPet.data );
//					bc.actionHandler.setID( mainPlayer.attackPet.data.Gender );
//					bc.actionHandler.setActionID( GameAction.STAND , null );
//					bc.setDirection( MapDirection.NORTH );
//					BattleSession.instance.addPet( bc );
//					bc.index = mainPlayerIndex;
//				}
			}
			
			
			for ( i = 0 ; i < numPet ; i++ )
			{
				bc = new BattleCreature();
				bc.init( null );
				
				readCreature( data , bc );

				bc.setDirection( MapDirection.NORTH );
				bc.setActionID( bc.data.Index );
				bc.setAction( GameAction.STAND , null );
				
				BattleSession.instance.addPet( bc );
			}
			
			for ( i = 0; i < numEnemy; i++ )
			{
				bc = new BattleCreature();
				bc.init( null );
				
				readCreature( data , bc );
				
				if ( bc.data.Index == GameDefine.INVALIDID )
				{
					bc.data.Index = 100000;
				}
				
				bc.setDirection( MapDirection.SOUTH );
				bc.setActionID( bc.data.Index );
				bc.setAction( GameAction.STAND , null );
				

				BattleSession.instance.addEnemy( bc );
			}
			
			for ( i = 0 ; i < numEnemyPet; i++ )
			{
				bc = new BattleCreature();
				bc.init( null );
				
				readCreature( data , bc );
				
				bc.setDirection( MapDirection.SOUTH );
				bc.setActionID( bc.data.Index );
				bc.setAction( GameAction.STAND , null );
				
				
				BattleSession.instance.addEnemyPet( bc );
			}
			
			mainPlayer.state = GameCeatureState.FIGHT;
			BattleSession.instance.startFight( pvp , direction , numPlayer , numPet , numEnemy , numEnemyPet );
		}
		
		
	}
	
	
}